package com.qk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.PageResult;
import com.qk.entity.Course;
import com.qk.mapper.CourseMapper;
import com.qk.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CourseServiceImpl implements CourseService {
    @Autowired
    private CourseMapper courseMapper;
    @Override
    public PageResult<Course> listCourses(Course course) {
        PageHelper.startPage(course.getPage(),course.getPageSize());
        List<Course> courses = courseMapper.listCourses(course);
        Page<Course> pageInfo = (Page<Course>) courses;
        return new PageResult<>(pageInfo.getTotal(), pageInfo.getResult());
    }

    @Override
    public void deleteCourse(Integer id) {
        courseMapper.deleteById(id);
    }

    @Override
    public void addCourse(Course course) {
        course.setCreateTime(LocalDateTime.now());
        course.setUpdateTime(LocalDateTime.now());
        courseMapper.addCourse(course);
    }

    @Override
    public Course getCourseById(Integer id) {
        return courseMapper.getCourseById(id);

    }

    @Override
    public void updateCourse(Course course) {
        courseMapper.updateCourse(course);

    }

    @Override
    public List<Course> getAllCourses() {
        return courseMapper.getAllCourses();

    }

    @Override
    public List<Course> listCoursesBySubject(Integer subject) {
        List<Course> courses = courseMapper.listCoursesBySubject(subject);
        return courses;
    }


}
